<template>
    <BasicModal
      v-bind="$attrs"
      @register="registerModal"
      show-footer
      width="40%"
      @ok="handleSubmit"
    >
      <template #title>

      </template>
      <template #footer>
        <a-button @click="closeModal" :loading="btnLoading">取消</a-button>
        <a-button
          type="primary"
          @click="handleSubmit(false)"
          :loading="btnLoading"
          >保存</a-button
        >
      </template>
      <BasicTable @register="registerTable" />
    </BasicModal>
  </template>
  <script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicTable, useTable } from '@/components/Table';

  import { useMessage } from '@/hooks/web/useMessage';
  import { useThrottleFn } from '@vueuse/core';
  import { defHttp } from '@/utils/http/axios';
  import dayjs from 'dayjs';
  const emit = defineEmits(['success', 'register']);
  const btnLoading = ref(false);
  const { createMessage } = useMessage();
  const [registerTable, { getSelectRowKeys, clearSelectedRowKeys }] =
    useTable({
      api: '/admin/groups/give_coupons',
      columns: [
        {
            title: 'ID',
            dataIndex: 'id',
            width: 50,
        },
        {
            title: '优惠券名称',
            dataIndex: 'name',
            width: 90,
        }, 
        {
            title: '金额',
            dataIndex: 'amount',
            width: 90,
        },
        {
            title: '生效日期',
            dataIndex: 'startDate',
            width: 90,
            customRender: ({ text }) => {
              return dayjs(text).format('YYYY-MM-DD');
            }
        },
        {
            title: '失效日期',
            dataIndex: 'endDate',
            width: 90,
            customRender: ({ text }) => {
              return dayjs(text).format('YYYY-MM-DD');
            }
        },
      ],
      useSearchForm: true,
      formConfig: {
        baseColProps: {
          span: 24
        },
        schemas: [
          {
            component: 'Input',
            label: '优惠券名称',
            field: 'keyword',
          },
        ]
      },
      showIndexColumn: false,
      indexColumnProps: {
        fixed: 'left',
      },
      pagination: true,
      bordered: true,
      actionColumn: {
        width: 150,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: 'right',
      },
      rowKey: 'id',
      rowSelection: {
        type: 'checkbox',
      },
    });
  const groupId = ref(null);
  const [registerModal, { closeModal, changeLoading }] =
  useModalInner(async (data) => {
    clearSelectedRowKeys();
    groupId.value = data.id
  });
  const handleSubmit = useThrottleFn(doSubmit, 1000);
  async function doSubmit() {
    const ids = getSelectRowKeys();
    if (ids.length === 0) {
      createMessage.error('请选择优惠券');
      return;
    }
    try {
      changeLoading(true);
      const res = await defHttp.post({ url: `/admin/groups/send_coupons/${groupId.value}`, data: getSelectRowKeys() });
      const { data } = res;
      createMessage.success(`发送给${data.memberCount}个会员${data.couponTypeCount}种类型的券共计${data.couponsCount}张`);
      emit('success');
      closeModal();
    } finally {
      changeLoading(false);
    }
  }
  </script>
  